package com.zmn.uac.business.enums;

import com.zmn.consts.GlobalConsts;
import com.zmn.uac.business.impl.oauth.login.*;
import com.zmn.uac.business.interfaces.oauth.login.LoginBaseBService;
import com.zmn.uac.common.dict.ThirdTypeDict;
import com.zmn.uac.common.info.AuthLoginCode;
import com.zmn.uac.common.info.UserAuthPhoneInfo;
import com.zmn.uac.common.info.alipay.AliPayUserAuthPhoneInfo;
import com.zmn.uac.common.info.apple.AppleUserAuthInfo;
import com.zmn.uac.common.info.baidu.BaiduUserAuthPhoneInfo;
import com.zmn.uac.common.info.bytebeat.ByteBeatAuthPhoneInfo;
import com.zmn.uac.common.info.kuaishou.KuaiShouAuthPhoneInfo;
import com.zmn.uac.common.info.wechat.WeChatUserAuthPhoneInfo;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;
import java.util.stream.Stream;


/**
 * 类描述：三方信息枚举
 *
 * @author xujie
 * @since 2021/06/14 14:13
 */
@Slf4j
public enum ThirdInfoEnum {

    /**
     *
     */
    ALI_PAY(ThirdTypeDict.ALI_PAY_CODE, "支付宝", new AliPayLoginBServiceImpl(), AliPayUserAuthPhoneInfo.class),

    BAI_DU(ThirdTypeDict.BAI_DU_CODE, "百度", new BaiduLoginBServiceImpl(), BaiduUserAuthPhoneInfo.class),

    BYTE_BEAT(ThirdTypeDict.BYTE_BEAT_CODE, "字节跳动、抖音", new ByteDanceLoginBServiceImpl(), ByteBeatAuthPhoneInfo.class),

    DING_TALK(ThirdTypeDict.DING_TALK_CODE, "钉钉", new DingTalkLoginBServiceImpl(), AuthLoginCode.class),

    JD(ThirdTypeDict.JD_CODE, "京东", new JDLoginBServiceImpl(), AuthLoginCode.class),

    WE_CHAT(ThirdTypeDict.WE_CHAT_CODE, "微信小程序", new WeChatLoginBServiceImpl(), WeChatUserAuthPhoneInfo.class),

    WE_CHAT_OFFICIAL(ThirdTypeDict.WE_CHAT_OFFICIAL_CODE, "微信公众号", new WeChatOfficialAccountLoginBServiceImpl(), WeChatUserAuthPhoneInfo.class),

    APPLE(ThirdTypeDict.APPLE_CODE, "苹果", new AppleLoginBServiceImpl(), AppleUserAuthInfo.class),

    KUAI_SHOU(ThirdTypeDict.KUAI_SHOU_CODE, "快手", new KuaiShouLoginBServiceImpl(), KuaiShouAuthPhoneInfo.class);

    /**
     * 三方类型
     */
    private final Integer thirdType;

    /**
     * 三方名称
     */
    private final String thirdName;

    /**
     * 登录基础业务
     */
    private final LoginBaseBService clazz;

    /**
     * 用户授权手机号加密信息基类
     */
    private final Class<? extends UserAuthPhoneInfo> userAuthPhoneInfoClazz;


    ThirdInfoEnum(Integer thirdType, String thirdName, LoginBaseBService clazz, Class<? extends UserAuthPhoneInfo> userAuthPhoneInfoClazz) {

        this.thirdType = thirdType;
        this.thirdName = thirdName;
        this.clazz = clazz;
        this.userAuthPhoneInfoClazz = userAuthPhoneInfoClazz;
    }

    public Integer getThirdType() {
        return thirdType;
    }

    public static LoginBaseBService getLoginBaseBService(Integer thirdType) {
        Optional<ThirdInfoEnum> enumOptional = Stream.of(ThirdInfoEnum.values()).filter(p -> p.thirdType.equals(thirdType)).findFirst();
        if (enumOptional.isPresent()) {
            return enumOptional.get().clazz;
        }
        log.error("[{}], 三方类型thirdType: ：{} 不存在 !", "三方信息枚举", thirdType);
        throw new IllegalArgumentException("三方类型thirdType: " + thirdType + " 不存在 !");
    }

    public static UserAuthPhoneInfo getUserAuthPhoneInfoClazz(Integer thirdType) {
        Optional<ThirdInfoEnum> enumOptional = Stream.of(ThirdInfoEnum.values()).filter(p -> p.thirdType.equals(thirdType)).findFirst();
        if (enumOptional.isPresent()) {
            try {
                return enumOptional.get().userAuthPhoneInfoClazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                log.error("[{}], 用户授权手机号加密信息基类初始化异常：{}", "三方信息枚举", e.getMessage(), e);
            }
        }
        log.error("[{}], 三方类型thirdType: ：{} 不存在 !", "三方信息枚举", thirdType);

        return new UserAuthPhoneInfo();
    }

    public static String getThirdName(Integer thirdType) {
        Optional<ThirdInfoEnum> enumOptional = Stream.of(ThirdInfoEnum.values()).filter(p -> p.thirdType.equals(thirdType)).findFirst();
        if (enumOptional.isPresent()) {
            return enumOptional.get().thirdName;
        }
        return GlobalConsts.SPLITER_EMPTY;
    }
}
